package com.dao;

import java.util.List;

import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.model.Customer;
import com.utility.HibenateUtil;

public class CustomerDAO  {
	private HibenateUtil hibenateUtil;

	public HibenateUtil getHibenateUtil() {
		return hibenateUtil;
	}

	public void setHibenateUtil(HibenateUtil hibenateUtil) {
		this.hibenateUtil = hibenateUtil;		
	}	

	public void saveOrUpdate(Customer customer) {
	
		//hibernateTemplate.save(customer);
         Session session =  hibenateUtil.getHibernateSessionFactory().openSession();
         org.hibernate.Transaction tr=session.beginTransaction();         
		 session.saveOrUpdate(customer);
		 tr.commit();
		 session.close();
	}

	public List<Customer> fetchAllCustomer() {
		//return hibernateTemplate.loadAll(Customer.class);
		Session session =  hibenateUtil.getHibernateSessionFactory().openSession();
		List<Customer> custormerList=session.createCriteria(Customer.class).list();
		session.close();		
		return custormerList;
		
	}
	
	public void delete(String id){
		Customer customer =getCustomerById(id);
		Session session =  hibenateUtil.getHibernateSessionFactory().openSession();
        org.hibernate.Transaction tr=session.beginTransaction();		
		session.delete(customer);
		tr.commit();
		session.close();
	}
	
	public Customer getCustomerById(String id){
		Session session =  hibenateUtil.getHibernateSessionFactory().openSession();        
		Customer customer =(Customer) session.get(Customer.class,Long.parseLong(id));
		session.close();
		return customer;
	}
}
